import time
from thing import *
import calculator
import action
import color
from textwrap import wrap

# List of skills the player can learn to improve his/her character
POSSIBLE_SKILLS = ['lockpicking', 'marksman']

def training(actor, skill):
    """ Train actor in specified skill. Currently does not check for trainer in
        the room, but NPCs have the necessary requirements for this to happen
        and can be added to NPCs with the 'editnpc <name>' command"""
    room = fetch(actor.location)
    if skill == "":
        actor.hear("Please specify a skill.", color.red)
        return
    if skill in POSSIBLE_SKILLS:
        actor.skills[skill] = 9
        actor.hear("You're now trained in "+skill+", at the ability of "+str(actor.skills[skill])+".", color.green)
    else:
        actor.hear("Huh?", color.red)

def greetNPC(actor, target):
    room = fetch(actor.location)
    npc = findItem(actor, target)
    # Basic greet
    for flag in npc.quests: # Iterate over every quest NPC offers until one is found the actor hasn't completed
        if flag in actor.Qflags: continue
        else:
            for quest in GREETINGS:  # Find the quest based on its flag
                if flag in quest[0]: # quest[0] == string of text, such as 'hello', or 'helloAgain'.
                    quest[1](actor) # this == firstHello(actor), as quest[1] will be a function.
                    return
def askNPC(actor, cmd):
    """ Function for progressing through quests associated with an NPC
        Call this for everything beyond the initial greeting and
        "We've met before" message."""
    target,sep,request = cmd.partition(' ')
    room = fetch(actor.location)
    npc = findItem(actor, target)
    for flag in npc.quests: # Iterate over every quest NPC offers until one is found the actor hasn't completed
        if flag in actor.Qflags: continue
        else:
            for quest in QUESTS:  # Find the quest based on its flag
                if flag in quest[0]: # quest[0] == string of text, such as 'hello', or 'helloAgain'.
                    quest[1](actor) # this == firstHello(actor), as quest[1] will be a function.
                    return
    

# The list of commands associated with NPC interaction, mostly regarding quests
commands = [
    (['greet', 'gree', 'gre', 'gr'], greetNPC),
    (['ask'], askNPC),
]

def displayText(actor, textRaw):
    "Format greetingRaw, and display to actor"
    textForm = wrap(textRaw, 70)
    for line in textForm:
        actor.hear(line, color.darkgreen)
    actor.hear("")
def itemCheck(actor, itemToFind):
    has = False
    for uid in actor.contents:
        item = fetch(uid)
        if item == None: continue
        if item.name == itemToFind:
            has = True
            item.moveInto(-1)
            del item
            break
    if has == False: return False
    else: return True
    

# Quest functions will go here. This module is going to be the HUGEST when we're done, by far.


def firstHello(actor):
    # Dialogue for quest in raw form - must be 'textRaw'
    textRaw = (
        "Hello there %s! You look exhausted. You must have been coding all afternoon, trying " # space here or at beginning of newline
        "to figure out how you were going to make this questing system work. Well, I think you've "
        "got it, so no worries there, mate!"%actor.name)
    # Call method to format and display dialogue
    displayText(actor, textRaw)
    # Append flag to actor so they can not repeat this quest stage
    actor.Qflags.append('hello')

def secondHello(actor):
    textRaw = (
        "We've already been introduced, in case you've forgotten.")
    displayText(actor, textRaw)
    # Do not append this flag to actor.Qflags to allow this text to be displayed from this point on.
    # But we can easily continue the dialogue with some copypasta action.
    textRaw = (
        "Why don't you go be productive? Go kill something...")
    # Maybe add some delay?
    time.sleep(1.5)
    displayText(actor, textRaw)

def getTheEgg1(actor): # Sample quest
    textRaw = (
        "Alright %s. There's an ancient magical egg I need retrieved. Go and get it, and I'll "
        "reward you handsomely."%actor.name)
    displayText(actor, textRaw)
    actor.Qflags.append('get the egg')

def getTheEgg2(actor):
    has = itemCheck(actor, 'magical egg')
    if has:
        textRaw = (
            "Well done, %s! Here's 10 gil that I'm sure you'll "
            "find a use for. Job well done!"%actor.name)
        displayText(actor, textRaw)
        actor.gil += 10
        actor.Qflags.append('egg acquired')
    else:
        textRaw = (
            "Retrieve the egg, and THEN I will give you the reward. Not before!")
        displayText(actor, textRaw)

def getTheEgg3(actor):
    textRaw = (
        "Yes, and thanks again! This egg will sell for a pretty penny! Muahaha!")
    displayText(actor, textRaw)

def getTheSword1(actor): # Sample quest
    displayText(actor,
        "There's a super sweet rocking awesome sword out there that I need you to fetch for me "
        "so that I can protect myself whilst galavanting around!")
    actor.Qflags.append('get the sword')

def getTheSword2(actor):
    has = itemCheck(actor, 'sweet sword')
    if has:
        displayText(actor,
            "Wonderful! I shall galavant to my galavanting heart's content! And in safety, I might add. "
            "Thank you, %s. I suppose I shall need to give you compensation. Here's a gil. Don't spend "
            "it all in one place."%actor.name)
        actor.gil += 1
        actor.Qflags.append('sweet sword returned')
    else:
        displayText(actor,
            "Hmm? I see no sword. I wish to galavant! Hurry! Find the sword, and perhaps we shall "
            "galavent together.")
def getTheSword3(actor):
    displayText(actor,
        "HAHAH! I simply LOVE galavanting!")



# Add quest by flag, then function to call the quest. This is for EVERY quest.
GREETINGS = [
    (['hello'], firstHello),
    (['helloAgain'], secondHello),
]

QUESTS = [
    (['getTheEgg1'], getTheEgg1), 
    (['getTheEgg2'], getTheEgg2),
    (['getTheEgg3'], getTheEgg3),
    (['getTheSword1'], getTheSword1),
    (['getTheSword2'], getTheSword2),
    (['getTheSword3'], getTheSword3),
]
